# -*- coding: utf-8 -*-
# @Author : Yuan
from rest_framework.generics import ListAPIView, DestroyAPIView
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from apps.meiduo_admin.utils import PageNum
from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from apps.meiduo_admin.serializers.serializers import SKUModelSerializer, GoodsCategoryModelSerializer, \
    SPUModelSerializer, SPUSpecificationModelSerializer


class SKUModelViewSet(ModelViewSet):
    # queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer
    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        return SKU.objects.all()


class GoodsCategoryListAPIView(ListAPIView):
    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = GoodsCategoryModelSerializer


class SPUListView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUModelSerializer


class SPUSpecificationListAPIView(ListAPIView):
    serializer_class = SPUSpecificationModelSerializer

    def get_queryset(self):
        spu_id = self.kwargs.get('spu_id')
        return SPUSpecification.objects.filter(spu_id=spu_id)



